package com.wyy.test.casetest;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @Date: 2023/4/18
 * @Author: wyy
 */
public class Case<T> {

    private Supplier<T> fetchData;
    private Runnable runnable;

    private Consumer<T> checkData;
    private String caseName;

    public Case(String caseName) {
        this.caseName = caseName;
    }


    public static <T> Case<T> create(String name) {
        return new Case<>(name);
    }

    public Case<T> run(Runnable run) {
        this.runnable = run;
        return this;
    }

    public <U> Case<U> fetchData(Supplier<U> fetchData) {
        Case<U> c = new Case<>(this.caseName);
        c.fetchData = fetchData;
        c.runnable = this.runnable;
        c.checkData = (Consumer<U>) this.checkData;
        return c;
    }

    public Case<T> checkData(Consumer<T> checkData) {
        Case<T> c = new Case<>(this.caseName);
        c.fetchData = this.fetchData;
        c.runnable = this.runnable;
        c.checkData = checkData;
        return c;
    }

    public Case<T> start() {
        runnable.run();
        T data = fetchData.get();
        checkData.accept(data);
        return this;
    }

    public Case<T> next(String caseName) {
        this.caseName = caseName;
        return this;
    }
}
